package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2025-01-14
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备的接口")
public class DeviceController extends BaseController {

    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @GetMapping("/list")
    @ApiOperation("查询设备列表")
    public TableDataInfo list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }


    /**
     * 从物联网平台同步产品列表
     *
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation("从物联网平台同步产品列表")
    public AjaxResult syncProductList() {
        deviceService.syncProductList();
        return success();
    }

    /**
     * 查询所有产品列表
     *
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation("查询所有产品列表")
    public AjaxResult allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return success(list);
    }

    /**
     * 注册设备
     *
     * @param deviceDto
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册设备")
    public AjaxResult register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return success();
    }

    /**
     * 查询设备详细数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("/{iotId}")
    @ApiOperation("查询设备详细数据")
    public AjaxResult getDetail(@PathVariable String iotId) {
        DeviceDetailVo deviceDetailVo = deviceService.getDetail(iotId);
        return success(deviceDetailVo);
    }

    /**
     * 查看设备上报的数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查看设备上报的数据")
    public AjaxResult queryServiceProperties(@PathVariable String iotId) {
        List<Map<String, Object>> list = deviceService.queryServiceProperties(iotId);
        return success(list);
    }

    /**
     * 修改设备
     *
     * @param deviceDto
     * @return
     */
    @PutMapping
    @ApiOperation("修改设备")
    public AjaxResult updae(@RequestBody DeviceDto deviceDto) {
        deviceService.updateDevice(deviceDto);
        return success();
    }

    /**
     * 删除设备
     *
     * @param iotId
     * @return
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public AjaxResult delete(@PathVariable String iotId) {
        deviceService.deleteDevice(iotId);
        return success();
    }
}
